package com.cskaoyan.market.admin.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.cskaoyan.market.admin.service.MarketAdminUserService;
import com.cskaoyan.market.admin.service.impl.MarketAdminUserServiceImpl;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

/**
 * 会员管理接口
 *
 * @author BlackFlame33
 * @date 2023/03/19
 **/
@WebServlet("/admin/*")
public class AdminUserController extends HttpServlet {
    private MarketAdminUserService marketAdminUserService = new MarketAdminUserServiceImpl();

    private static boolean isNotLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object info = req.getSession().getAttribute("info");
        if (info == null) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.unlogin()));;
            return true;
        }
        return false;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin", "");
        switch (op) {
            case "/user/list":
                userList(req, resp);
                break;
            case "/address/list":
                addressList(req, resp);
                break;
            case "/collect/list":
                collectList(req, resp);
                break;
            case "/footprint/list":
                footPrintList(req, resp);
                break;
            case "/history/list":
                historyList(req, resp);
                break;
            case "/feedback/list":
                feedbackList(req, resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void userList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String username = req.getParameter("username");
        String mobileParam = req.getParameter("mobile");

        // 校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            // 这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        int mobile = 0;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (StringUtils.isNotEmpty(mobileParam)) {
                // 如果手机号不为空，那么也应当是一个整数
                mobile = Integer.parseInt(mobileParam);
            }
            if (StringUtils.isEmpty(username)) {
                username = "";
            }
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketUser> list = marketAdminUserService.getUserList(page, limit, sort, order, username, mobileParam);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }

    private void addressList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name = req.getParameter("name");
        String userIdParam = req.getParameter("userId");
        // 校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            // 这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        int userId = -1;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (StringUtils.isNotEmpty(userIdParam)) {
                // 如果用户ID不为空，那么也应当是一个整数
                userId = Integer.parseInt(userIdParam);
            }
            if (StringUtils.isEmpty(name)) {
                name = "";
            }
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketAddress> list = marketAdminUserService.getAddressList(page, limit, sort, order, name, userId);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));

    }

    private void collectList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String valueIdParam = req.getParameter("valueId");
        String userIdParam = req.getParameter("userId");
        // 校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            // 这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        int valueId = -1;
        int userId = -1;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (StringUtils.isNotEmpty(userIdParam)) {
                // 如果用户ID不为空，那么也应当是一个整数
                userId = Integer.parseInt(userIdParam);
            }
            if (StringUtils.isNotEmpty(valueIdParam)) {
                // 如果收藏的数据ID不为空，那么也应当是一个整数
                valueId = Integer.parseInt(valueIdParam);
            }
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketCollect> list = marketAdminUserService.getCollectList(page, limit, sort, order, userId, valueId);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }

    private void footPrintList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userIdParam = req.getParameter("userId");
        String goodsIdParam = req.getParameter("goodsId");
        // 校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            // 这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        int userId = -1;
        int goodsId = -1;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (StringUtils.isNotEmpty(userIdParam)) {
                // 如果用户ID不为空，那么也应当是一个整数
                userId = Integer.parseInt(userIdParam);
            }
            if (StringUtils.isNotEmpty(goodsIdParam)) {
                // 如果收藏的数据ID不为空，那么也应当是一个整数
                goodsId = Integer.parseInt(goodsIdParam);
            }
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketFootprint> list = marketAdminUserService.getFootprintList(page, limit, sort, order, userId, goodsId);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }

    private void historyList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userIdParam = req.getParameter("userId");
        String keyword = req.getParameter("keyword");
        // 校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            // 这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        int userId = -1;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (StringUtils.isNotEmpty(userIdParam)) {
                // 如果用户ID不为空，那么也应当是一个整数
                userId = Integer.parseInt(userIdParam);
            }
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketSearchHistory> list =
            marketAdminUserService.getHistoryList(page, limit, sort, order, userId, keyword);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }

    private void feedbackList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String idParam = req.getParameter("id");
        String username = req.getParameter("username");
        // 校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            // 这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        int id = -1;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (StringUtils.isNotEmpty(idParam)) {
                // 如果反馈 id 不为空，那么也应当是一个整数
                id = Integer.parseInt(idParam);
            }
            if (StringUtils.isEmpty(username)) {
                username = "";
            }
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketFeedback> list = marketAdminUserService.getFeedbackList(page, limit, sort, order, username, id);

        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }
}